//
//  SYMainController.swift
//  SYWeibo
//
//  Created by Saya on 16/4/6.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit

private let baseIndext:Int = 1000

class SYMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tabBarOfSelf = SYTabBar()
        self.setValue(tabBarOfSelf, forKey: "tabBar")
        tabBarOfSelf.plusBtn.addTarget(self, action: #selector(SYMainController.plusBtnDidClick), forControlEvents: .TouchUpInside)
        
        //添加子空间
        addViewControllers()
    }
    
    //
    @objc private func plusBtnDidClick(){
        print("plus button did click")
        let composeView = SYComposeView()
        composeView.show(self)
    }
    
    private func addViewControllers () {
        addViewController(SYHomeTBVC(), title: "首页", imageName: "tabbar_home",index:0)
        addViewController(SYMessageTBVC(), title: "消息", imageName: "tabbar_message_center",index:1)
        addViewController(SYDiscoverTBVC(), title: "发现", imageName: "tabbar_discover",index:2)
        addViewController(SYProfileTBVC(), title: "我", imageName: "tabbar_profile",index:3)
    }
    
    private func addViewController (VC: UIViewController, title:String, imageName:String,index:Int) {
        //下面这种方法可以将tab的标题和 navigation的标题设置的 不一致
        VC.tabBarItem.title = title
        VC.navigationItem.title = title
        //tab的图标
        VC.tabBarItem.image = UIImage(named: imageName)
        VC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)//swift中使用枚举的时候,以枚举类型名.枚举的值,在使用的时候枚举名可以忽略
        //字体颜色
        //上下俩个方法的区别
        //        tabBar.tintColor = UIColor.orangeColor()
        VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //vc的tag值
        VC.tabBarItem.tag = baseIndext + index
        
        let navigation = SYBaseNavController(rootViewController: VC)
        addChildViewController(navigation)
    }

    override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
//        super.tabBar(tabBar, didSelectItem: item)
        
        //mark index
        var index = 0
        for subview in tabBar.subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                if item.tag - baseIndext == index{
                    print("ok")
                }
                index++
            }
        }
    }
}
